#include "playscene.h"
#include "mypushbutton.h"
#include "mycoin.h"
#include "dataconfig.h"
#include <QAction>
#include <QMenuBar>
#include <QPainter>
#include <QLabel>
#include <QDebug>
#include <QPropertyAnimation>
#include <QTimer>
#include <QSound>
PlayScene::PlayScene(int levelNum){
    QSound * winSound = new QSound(":/res/LevelWinSound.wav",this);

    this->levelIndex = levelNum;
    this->setFixedSize(630,1176);
    this->setWindowTitle("翻金币啊");
    this->setWindowIcon(QIcon("://res/Coin0001.png"));
    QMenuBar * bar = menuBar();
    this->setMenuBar(bar);
    QMenu * startMenu = bar->addMenu("开始a");
    QAction * actionQuit = new QAction("退出a");
    startMenu->addAction(actionQuit);
    connect(actionQuit,&QAction::triggered,[=](){
       this->close();
    });

    MyPushButton * backBtn = new MyPushButton(":/res/BackButton.png",":/res/BackButtonSelected.png",2,2);
    backBtn->setParent(this);
    backBtn->move(this->width()-backBtn->width(),this->height()-backBtn->height());

    QLabel * WinLabel = new QLabel(this);
    QPixmap Winpix(":/res/LevelCompletedDialogBg.png");
    Winpix = Winpix.scaled(Winpix.width()*2,WinLabel->height()*6);
    WinLabel->setGeometry(0,0,Winpix.width(),Winpix.height());
    WinLabel->setPixmap(Winpix);
    WinLabel->move(this->width()/2-Winpix.width()/2,-Winpix.height());

    connect(backBtn,&QPushButton::clicked,[=](){
        QTimer::singleShot(200,this,[=](){
            emit backChooseScene();
        });
    });

    QString s = QString("Level:%1").arg(levelNum);
    QLabel * label = new QLabel(s,this);
    label->setFont(QFont("楷体",30,QFont::Bold));
    label->setGeometry(50,this->height()*0.8,400,200);
    //等价于label->setFixedSize(400,200);label->move(50,this->height()*0.8);

    dataConfig * config = new dataConfig();
    for(int i = 0;i < 4;i++){
        for(int j = 0;j < 4;j++){
            gameArray[i][j] = config->mData[levelNum][i][j];
        }
    }


    QString coinPath;
    for(int i = 0;i < 4;i++){
        for(int j = 0;j < 4;j++){
            QLabel * label = new QLabel(this);
            QPixmap pix;
            pix.load(":/res/BoardNode.png");
            pix = pix.scaled(pix.width()*2,pix.height()*2);
            label->setGeometry(125+i*100,425+j*100,pix.width(),pix.height());
            label->setPixmap(pix);
            label->setAlignment(Qt::AlignCenter);

            if(gameArray[i][j]==1){
                coinPath = ":/res/Coin0001.png";
            }else {
                coinPath = ":/res/Coin0008.png";
            }

            MyCoin * coin = new MyCoin(coinPath);
            coin->setParent(this);
            coin->posX = i;
            coin->posY = j;
            coin->flag =gameArray[i][j];
            this->coinBtn[i][j] = coin;
            connect(coin,&QPushButton::clicked,[=](){
                for(int i=0;i<4;i++){
                    for(int j=0;j<4;j++){
                        this->coinBtn[i][j]->isWin=true;
                    }
                }
                coin->changeFlag();
                this->gameArray[i][j] = this->gameArray[i][j] == 0? 1 : 0;
                QTimer::singleShot(70,this,[=](){
                    if(coin->posX+1<=3){
                        this->coinBtn[i+1][j]->changeFlag();
                        this->gameArray[i+1][j] = this->gameArray[i+1][j] == 0? 1 : 0;
                    }
                    if(coin->posX-1>=0){
                        this->coinBtn[i-1][j]->changeFlag();
                        this->gameArray[i-1][j] = this->gameArray[i-1][j] == 0? 1 : 0;
                    }
                    if(coin->posY-1>=0){
                        this->coinBtn[i][j-1]->changeFlag();
                        this->gameArray[i][j-1] = this->gameArray[i][j-1] == 0? 1 : 0;
                    }
                    if(coin->posY+1<=3){
                        this->coinBtn[i][j+1]->changeFlag();
                        this->gameArray[i][j+1] = this->gameArray[i][j+1] == 0? 1 : 0;
                    }

                    for(int i=0;i<4;i++){
                        for(int j=0;j<4;j++){
                            this->coinBtn[i][j]->isWin=false;
                        }
                    }

                    this->isWin = true;
                    for(int i=0;i<4;i++){
                        for(int j=0;j<4;j++){
                            if(this->coinBtn[i][j]->flag==false){
                                this->isWin = false;
                                break;
                            }
                        }
                    }
                    if(this->isWin){
                        for(int i=0;i<4;i++){
                            for(int j=0;j<4;j++){
                                this->coinBtn[i][j]->isWin = true;
                            }
                        }
                        QPropertyAnimation * animation = new QPropertyAnimation(WinLabel,"geometry");
                        animation->setDuration(1000);
                        animation->setStartValue(QRect(WinLabel->x()+12.5,WinLabel->y(),WinLabel->width(),WinLabel->height()));
                        animation->setEndValue(QRect(WinLabel->x()+12.5,WinLabel->y()+400,WinLabel->width(),WinLabel->height()));
                        animation->setEasingCurve(QEasingCurve::OutBounce);
                        winSound->play();
                        animation->start();
                    }
                });
            });
            int x = (label->width() - coin->width()) / 2;
            int y = (label->height() - coin->height()) / 2;
            coin->move(label->pos() + QPoint(x, y));
        }
    }
}

void PlayScene::paintEvent(QPaintEvent *){
    QPainter painter(this);
    QPixmap pixmap;
    pixmap.load(":/res/PlayLevelSceneBg.png");
    painter.drawPixmap(0,0,this->width(),this->height(),pixmap);
    pixmap.load(":/res/Title.png");
    pixmap = pixmap.scaled(pixmap.width(),pixmap.height());
    painter.drawPixmap(10,100,pixmap);
}
